home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CExamples / Count.c next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  3.6 KB  |  158 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  
  3. NAME
  4.       Count -- count lines and characters
  5.  
  6. SYNOPSIS
  7.       Count [-l] [-c] [file…]
  8.  
  9. DESCRIPTION
  10.       "Count" counts the lines and characters in its input, and writes the
  11.         counts to standard output.  If no files are specified standard input is
  12.         read.  If more than one file is specified, separate counts are written
  13.         for each file, one per line, preceeded by the file name.  A total is also
  14.         written following the list of files.
  15.         
  16. COPYRIGHT
  17.       Copyright Apple Computer, Inc. 1985-1988, 1994
  18.         All rights reserved.
  19.  
  20. ------------------------------------------------------------------------------*/
  21.  
  22. #include    <Types.h>
  23. #include     <ctype.h>
  24. #include     <fcntl.h>
  25. #include    <errno.h>
  26. #include     <string.h>
  27. #include     <stdio.h>
  28. #include    <ErrMgr.h>
  29. #include    <CursorCtl.h>
  30. #include    <Errors.h>
  31.  
  32.  
  33. #define    InputSize    (1024*8)
  34.  
  35. /* Variables local to this file */
  36.  
  37. static    char    inputBuffer[InputSize + 1];
  38. static    char    errorBuffer[256];
  39.  
  40. static    char    *usage = "# Usage - %s [-l] [-c] [file…].\n";
  41. static    long    optionsSpecified;
  42. static    long    lineOption;
  43. static    long    charOption;
  44.  
  45. struct counts {
  46.     long    lines;
  47.     long    characters;
  48. };
  49.     
  50. struct counts count(FILE *input)
  51. {
  52.     char            c;
  53.     char            *ptr;
  54.     long            lines = 0;
  55.     long            characters = 0;
  56.     long            charsRead;
  57.     struct    counts    cnts;
  58.     
  59.     while ((charsRead = fread(inputBuffer, sizeof(char), InputSize, input)) > 0) {
  60.         ptr = inputBuffer;
  61.         inputBuffer[charsRead] = 0;
  62.         characters += charsRead;
  63.         while ((c = *ptr++) != 0 || ptr <= &inputBuffer[charsRead]) {
  64.             if (c == '\n') {
  65.                 lines++;
  66.                 if ((lines & 0x0F) == 0)
  67.                     SpinCursor(1);
  68.             }
  69.         }
  70.     }    
  71.     if (characters > 0 && *(ptr-2) != '\n')
  72.         lines++;
  73.    cnts.lines = lines;
  74.     cnts.characters = characters;
  75.     return cnts;
  76. }
  77.  
  78.  
  79. void print(long files, long max, char *name, struct counts cnts)
  80. {
  81.     long    space;
  82.     
  83.     space = 0;
  84.     if (files > 1) {
  85.         fprintf(stdout,"%s ", name);
  86.         space = max - strlen(name);
  87.     }
  88.     if (optionsSpecified == false || lineOption == true) {
  89.         fprintf(stdout, "%*d ", space + 5, cnts.lines);
  90.         space = 0;
  91.     }
  92.     if (optionsSpecified == false || charOption == true) {
  93.         fprintf(stdout, "%*d ", space + 7, cnts.characters);
  94.     }
  95.     fprintf(stdout, "\n");
  96.     fflush(stdout);
  97. }
  98.         
  99.  
  100. main(int argc, char *argv[])
  101. {
  102.     long            status;
  103.     long            parms;
  104.     long            files;
  105.     long            done;
  106.     long            length;
  107.     long            max;
  108.     FILE            *input;
  109.     struct    counts    cnts;
  110.     struct    counts    total;
  111.  
  112.     status = files = 0;
  113.     max = strlen("Total");
  114.     optionsSpecified = lineOption = charOption = false;
  115.     InitCursorCtl(nil);
  116.  
  117.     for (parms = 1; parms < argc; parms++) {
  118.         length = strlen(argv[parms]);
  119.         if (*argv[parms] != '-') {
  120.             argv[++files] = argv[parms];
  121.             if (max < length)
  122.                 max = length;
  123.         } else if (tolower(*(argv[parms]+1)) == 'c' && length == 2) {
  124.             optionsSpecified = charOption = true;
  125.         } else if (tolower(*(argv[parms]+1)) == 'l' && length == 2) {
  126.             optionsSpecified = lineOption = true;
  127.         } else {
  128.             fprintf(stderr,"### %s - \"%s\" is not an option.\n", argv[0], argv[parms]);
  129.             fprintf(stderr, usage, argv[0]);
  130.             return 1;
  131.         }
  132.     }
  133.     if (files == 0) {
  134.         cnts = count(stdin);
  135.         print(files, max, NULL, cnts);
  136.     } else {
  137.         total.lines = total.characters = done = 0;
  138.         for (parms = 1; parms <= files; parms++) {
  139.             if ((input = fopen(argv[parms], "r")) != NULL) {
  140.                 cnts = count(input);
  141.                 fclose(input);
  142.                 total.lines += cnts.lines;
  143.                 total.characters += cnts.characters;
  144.                 print(files, max, argv[parms], cnts);
  145.                 done++;
  146.             } else {
  147.                 fprintf(stderr,"### %s - Unable to open file %s.\n", argv[0], argv[parms]);
  148.                 fprintf(stderr,"# %s\n", GetSysErrText(MacOSErr, errorBuffer));
  149.                 status = 2;
  150.             }
  151.         }
  152.         if (done > 1) {
  153.             print(files,max,"Total",total);
  154.         }
  155.     }
  156.     return status;
  157. }
  158.